package com.rpm.rpm.login.login;

import com.alibaba.fastjson.JSONObject;
import com.rpm.rpm.constant.ExceptionConstant;
import com.rpm.rpm.mapper.LoginDao;
import com.rpm.rpm.util.SnowFlakeGenerateIdWorker;
import com.rpm.rpm.vo.ResultVO;
import com.rpm.rpm.vo.UserVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 登录综合业务层
 */
@Service
public class LoginTransService {

    @Autowired
    LoginDao dao;

    /**
     * 登录方法
     * @param param
     * @return
     */
    public ResultVO login(JSONObject param,ResultVO resultVO){
        //用户号
        String userCode = param.getString(LoginConstant.userCode);
        //密码
        String userPassword = param.getString(LoginConstant.userPassword);
        UserVO userVO = dao.queryLoginByUserAndPassword(userCode,userPassword);
        if (null == userVO){
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badLogin + LoginExceptionConstant.accountOrPasswordError);
            return resultVO;
        }
        return resultVO;
    }

    /**
     * 登录判空方法
     * @param param
     * @param resultVO
     * @return
     */
    public ResultVO judgingEmptinessByLogin(JSONObject param,ResultVO resultVO){
        //用户号
        String userCode = param.getString(LoginConstant.userCode);
        if (StringUtils.isEmpty(userCode)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badLogin + LoginExceptionConstant.mustUserCode);
            return resultVO;
        }
        //密码
        String userPassword = param.getString(LoginConstant.userPassword);
        if (StringUtils.isEmpty(userPassword)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badLogin + LoginExceptionConstant.mustUserName);
            return resultVO;
        }
        return resultVO;
    }

    /**
     * 转换为用户实体方法
     * @param userVO
     * @param param
     * @return
     */
    public UserVO jsonTransByUserVO(UserVO userVO,JSONObject param){
        if (null == userVO){
            userVO = new UserVO();
        }
        //员工号
        userVO.setUserCode(param.getString(LoginConstant.userCode));
        //密码
        userVO.setUserPassword(param.getString(LoginConstant.userPassword));
        //用户名称
        userVO.setUserName(param.getString(LoginConstant.userName));
        //电话
        String phone = param.getString(LoginConstant.phone);
        if (StringUtils.isNotEmpty(phone)) {
            userVO.setPhone(phone);
        }
        //地址
        String address = param.getString(LoginConstant.address);
        if (StringUtils.isNotEmpty(address)) {
            userVO.setAddress(address);
        }
        //所属院校
        String college = param.getString(LoginConstant.college);
        if (StringUtils.isNotEmpty(college)) {
            userVO.setCollege(college);
        }
        //职称
        String rank = param.getString(LoginConstant.rank);
        if (StringUtils.isNotEmpty(rank)) {
            userVO.setRank(rank);
        }
        //公测码
        String publicBate = param.getString(LoginConstant.publicBate);
        //权限
        if (StringUtils.isNotEmpty(publicBate)) {
            userVO.setPublicBate(publicBate);
            //院级管理员权限码
            if (publicBate.equals(LoginConstant.hospitalLevel)){
                userVO.setAuthority(1);
            }
            //校级科研管理员权限码
            else if (publicBate.equals(LoginConstant.schoolLevel)) {
                userVO.setAuthority(2);
            }
            //评审专家权限
            else {
                userVO.setAuthority(3);
            }
        }
        else {
            //普通教职工
            userVO.setAuthority(0);
        }
        return userVO;
    }

    /**
     * 判空方法
     * @param param
     * @param resultVO
     * @return
     */
    public ResultVO judgingEmptinessByRegister(JSONObject param,ResultVO resultVO){
        //员工号
        String userCode = param.getString(LoginConstant.userCode);
        if (StringUtils.isEmpty(userCode)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(LoginExceptionConstant.mustUserCode);
            return resultVO;
        }
        //判断员工号是否有四个字母和八个数字并且长度不能超过12位
        if (lengthLetterDigit(userCode,resultVO).getCode().equals(ExceptionConstant.five)) {
            return resultVO;
        }
        //密码
        String userPassword = param.getString(LoginConstant.userPassword);
        if (StringUtils.isEmpty(userPassword)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(LoginExceptionConstant.mustUserPassword);
            return resultVO;
        }
        //用户名称
        String userName = param.getString(LoginConstant.userName);
        if (StringUtils.isEmpty(userName)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(LoginExceptionConstant.mustUserName);
            return resultVO;
        }
        //电话
        String phone = param.getString(LoginConstant.phone);
        if (StringUtils.isEmpty(phone)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badRegister + LoginExceptionConstant.mustPhone);
            return resultVO;
        }
        //地址
        String address = param.getString(LoginConstant.address);
        if (StringUtils.isEmpty(address)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badRegister + LoginExceptionConstant.mustAddress);
            return resultVO;
        }
        //所属院校
        String college = param.getString(LoginConstant.college);
        if (StringUtils.isEmpty(college)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badRegister + LoginExceptionConstant.mustCollege);
            return resultVO;
        }
        //职称
        String rank = param.getString(LoginConstant.rank);
        if (StringUtils.isEmpty(rank)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badRegister + LoginExceptionConstant.mustRank);
            return resultVO;
        }
        return resultVO;
    }

    /**
     * 判断长度是否超过12，并且包含4个字母，8个数字
     * @param userCode
     * @return
     */
    public ResultVO lengthLetterDigit(String userCode,ResultVO resultVO){
        //判断长度是否超过12位
        if (userCode.length() > 12){
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(LoginExceptionConstant.lengthTooLong);
            return resultVO;
        }
        //是否包含4个字母
        String letter = userCode.replaceAll("[^a-zA-Z]", "");
        if (letter.length() < 4){
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(LoginExceptionConstant.mustFourLetter);
            return resultVO;
        }
        //是否包含8个数字
        String num = userCode.replaceAll("[^0-9]", "");
        if (num.length() < 8){
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(LoginExceptionConstant.mustEightNum);
            return resultVO;
        }
        return resultVO;
    }
}
